﻿using System;
using System.Runtime.Serialization;

namespace AjaxControlToolkit
{
    [Serializable]
    public class UpdatePanelExtenderEventArgs : EventArgs, ISerializable
    {
        private readonly string _executionResult;
        private readonly ExecutionResultState _executionResultState;
        private readonly DateTime _startExecutionTime;
        private readonly DateTime _stopExecutionTime;
        private readonly int _totalSeconds;
        
        public UpdatePanelExtenderEventArgs()
        {
            _executionResult = "";
            _executionResultState = ExecutionResultState.Undefined;
            _startExecutionTime = DateTime.Now;
            _stopExecutionTime = DateTime.Now;
        }

        protected UpdatePanelExtenderEventArgs(SerializationInfo info, StreamingContext context)
        {
            _executionResult = info.GetString("ExecutionResult");
            _executionResultState = (ExecutionResultState)info.GetInt32("ExecutionResultState");
            _startExecutionTime = info.GetDateTime("StartExecutionTime");
            if ((_executionResultState == ExecutionResultState.Error) || (_executionResultState == ExecutionResultState.Error))
            {
                _stopExecutionTime = info.GetDateTime("StopExecutionResult");
            }
            else
            {
                _stopExecutionTime = DateTime.Now;
            }
        }

        public UpdatePanelExtenderEventArgs(string executionResult, DateTime startExecutionTime, ExecutionResultState executionResultState)
        {
            _executionResult = executionResult;
            _startExecutionTime = startExecutionTime;
            _executionResultState = executionResultState;
            _stopExecutionTime = DateTime.Now;
        }

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.SetType(typeof(UpdatePanelExtenderEventArgs));
            info.AddValue("ExecutionResult", _executionResult);
            info.AddValue("StartExecutionTime", _startExecutionTime);
            info.AddValue("ExecutionResultState", (int)_executionResultState);
            if ((_executionResultState == ExecutionResultState.Error) || (_executionResultState == ExecutionResultState.Error))
            {
                info.AddValue("StopExecutionResult", DateTime.Now);
            }
        }

        public string ExecutionResult
        {
            get
            {
                return _executionResult;
            }
        }

        public DateTime StartExecutionTime
        {
            get
            {
                return _startExecutionTime;
            }
        }
        
        public TimeSpan ExecutionTime
        {
            get
            {
                return _stopExecutionTime - _startExecutionTime;
            }
        }

        public ExecutionResultState ExecutionResultState
        {
            get
            {
                return _executionResultState;
            }
        }

        public int TotalSeconds
        {
            get
            {
                return (int)ExecutionTime.TotalSeconds;
            }
        }
    }
}
